package com.tico.editor.editors.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tico.common.pojo.Result;
import com.tico.common.pojo.ResultHelper;
import com.tico.common.utils.BaseQuery;
import com.tico.common.utils.JsonUtils;
import com.tico.common.utils.QueryResult;
import com.tico.common.webResponse.WebResponse;
import com.tico.editor.editors.controller.websocket.HttpWebSocket;
import com.tico.editor.editors.domain.HttpEditor;
import com.tico.editor.editors.domain.KafkaTopic;
import com.tico.editor.editors.param.HttpEditorParam;
import com.tico.editor.editors.service.HttpEditorService;
import com.tico.editor.editors.service.PubilcDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/http")
@Slf4j
public class HttpController {
    @Autowired
    private HttpEditorService httpEditorService;
    @Autowired
    private PubilcDataService pubilcDataService;

    @PostMapping(value = "/test")
    public WebResponse test(@RequestBody HttpEditor httpEditor, HttpServletRequest request) {
        WebResponse wr = new WebResponse();
        httpEditor.setUrl(pubilcDataService.translate(httpEditor.getUrl()));
        httpEditor.setHeaders(pubilcDataService.translate(httpEditor.getHeaders()));
        httpEditor.setBody(pubilcDataService.translate(httpEditor.getBody()));
        JSONObject resp  = httpEditorService.postman(httpEditor);
        try {
            HttpWebSocket.sendMessage(httpEditor.getRandomNum(), JSON.toJSONString(resp));
        } catch (IOException e) {
            e.printStackTrace();
            wr.setSuccess(false);
            wr.setErr_msg("失败");
        }
        wr.setSuccess(true);
        wr.setErr_msg("成功");
        return wr;
    }
    @PostMapping(value = "/save")
    public JSONObject save(@RequestBody HttpEditor httpEditor) {
        Result res = new Result();
        try {
            httpEditor.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            httpEditor.setStatus("false");
            httpEditor.setReturnStatus("false");
            httpEditorService.insert(httpEditor);
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
    @GetMapping(value = "/list")
    public JSONObject list(HttpEditorParam httpEditor) {
        Result result = new Result();
        try {
            BaseQuery baseQuery = new BaseQuery();
            baseQuery.setPage(httpEditor.getPage());
            baseQuery.setLimit(httpEditor.getLimit());
            QueryResult<HttpEditor> pageInfo = httpEditorService.findPageInfo(baseQuery);
            result.addModel("count",pageInfo.getTotalRecord());
            result.addModel("data", JsonUtils.toUnderlineJSONFilterString(HttpEditor.class,pageInfo.getResultList(),new String[]{}) );
            result.setMessage("成功");
            result.setSuccess(true);
        }catch (Exception e){
            result.setMessage("失败");
            result.setSuccess(false);
        }
        return ResultHelper.renderAsJsonWipeData(result);
    }
    @PostMapping(value = "/delete")
    public JSONObject delete(@RequestBody HttpEditorParam httpEditorParam, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = httpEditorParam.getIds().split(",");
            for(int i=0;i<str.length;i++){
                httpEditorService.delete(str[i]);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
    @PostMapping(value = "/returnDataCheck")
    public JSONObject returnDataCheck(@RequestBody HttpEditor httpEditor, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = httpEditor.getIds().split(",");
            for(int i=0;i<str.length;i++){
                httpEditor.setId(str[i]);
                httpEditorService.update(httpEditor);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
    @PostMapping(value = "/startUsing")
    public JSONObject startUsing(@RequestBody HttpEditor httpEditor, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = httpEditor.getIds().split(",");
            for(int i=0;i<str.length;i++){
                httpEditor.setId(str[i]);
                httpEditorService.update(httpEditor);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
}
